package com.libraries.view;

import com.example.libraries.R;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;


public class TitleBarView {
	private static TitleBarView titleLayout;
	private Object parentView;
	private View v_network;
	private TextView tv_title;
	private Button btn_left, btn_right ;
	private ImageButton ibtn_right,ibtn_left;

	public static TitleBarView getInstance() {
		if (titleLayout == null) {
			titleLayout = new TitleBarView();
		}
		return titleLayout;
	}

	/**
	 * 可以试颜色也可以是图片
	 * 
	 * @param resid
	 * @param color
	 */
	public void setBackground(int resid, int color) {
		if (resid != 0) {
			if (parentView instanceof Activity){
				((Activity) parentView).findViewById(R.id.rl_top_layout).setBackgroundResource(resid);
			}else{
				((View) parentView).findViewById(R.id.rl_top_layout).setBackgroundResource(resid);
			}
		} else {
			if (parentView instanceof Activity){
				((Activity) parentView).findViewById(R.id.rl_top_layout).setBackgroundColor(color);
			}else{
				((View) parentView).findViewById(R.id.rl_top_layout).setBackgroundColor(color);
			}
		}
	}
	
	public int getTitleBarHeight(){
		if (parentView==null) {
			return -1;
		}
		if (parentView instanceof Activity){
			return ((Activity) parentView).findViewById(R.id.ll_titlebar).getHeight();
		}else{
			((View) parentView).findViewById(R.id.ll_titlebar).getHeight();
		}
		return -1;
	}

	/**
	 * 
	 * @param parentView 
	 * @param title
	 * @param color
	 * @return
	 */
	public TextView createTitle(Object parentView, Object title, int color) {
		this.parentView = parentView;
		if (parentView instanceof Activity){
			tv_title = (TextView) ((Activity) parentView).findViewById(R.id.tv_top_title);
		}else{
			tv_title = (TextView) ((View) parentView).findViewById(R.id.tv_top_title);
		}
		if (title != null) {
			if (title instanceof Integer) {
				if ((Integer) title != 0) {
					tv_title.setVisibility(View.VISIBLE);
					tv_title.setText((Integer) title);
				}
			} else {
				tv_title.setVisibility(View.VISIBLE);
				tv_title.setText((CharSequence) title + "");
			}
			if (color != 0) {
				tv_title.setTextColor(color);
			}
		}
		return tv_title;
	}

	/**
	 * 
	 * @param text 文字  null表示不设置
	 * @param d 图片 null表示不设置
	 * @param color 文字颜色 0 表示不设置
	 * @return
	 */
	public Button createLeftButton(Object text, Drawable d, int color) {
		if (parentView instanceof Activity){
			btn_left = (Button) ((Activity) parentView).findViewById(R.id.btn_top_left);
		}
		else{
			btn_left = (Button) ((View) parentView).findViewById(R.id.btn_top_left);
		}
		if (text != null) {
			if (text instanceof Integer) {
				if ((Integer) text != 0) {
					btn_left.setVisibility(View.VISIBLE);
					btn_left.setText((Integer) text);
				}
			} else {
				btn_left.setVisibility(View.VISIBLE);
				btn_left.setText((CharSequence) text + "");
			}
			if (color != 0) {
				btn_left.setTextColor(color);
			}
		}
		if (d != null) {
			d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
			btn_left.setCompoundDrawables(d, null,null,null);
		}
		return btn_left;
	}
	
	/**
	 *
	 * @param text 文字  null表示不设置
	 * @param d 图片 null表示不设置
	 * @param color 文字颜色 0 表示不设置
	 * @return
	 */
	public Button createRightButton(Object text, Drawable d, int color) {
		if (parentView instanceof Activity){
			btn_right = (Button) ((Activity) parentView).findViewById(R.id.btn_top_right);
		}else {
			btn_right = (Button) ((View) parentView).findViewById(R.id.btn_top_right);
		}
		btn_right.setVisibility(View.VISIBLE);
		if (text != null) {
			if (text instanceof Integer) {
				if ((Integer) text != 0) {
					btn_right.setVisibility(View.VISIBLE);
					btn_right.setText((Integer) text);
				}
			} else {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setText((CharSequence) text + "");
			}
			if (color != 0) {
				btn_right.setTextColor(color);
			}
		}
		if (d != null) {
			d.setBounds(0, 0, d.getMinimumWidth(), d.getMinimumHeight());
			btn_right.setCompoundDrawables(null, null,d, null);
		}
		return btn_right;
	}
	

	/**
	 * 可以是文字/图片资源
	 * 
	 * @param resid
	 */
	public ImageButton createLeftImageButton(int drawable) {
		if (parentView instanceof Activity){
			ibtn_left = (ImageButton) ((Activity) parentView).findViewById(R.id.ibtn_top_left);
		}else{
			ibtn_left = (ImageButton) ((View) parentView).findViewById(R.id.ibtn_top_left);
		}
		if (drawable != 0) {
			ibtn_left.setVisibility(View.VISIBLE);
			ibtn_left.setImageResource(drawable);
		}
		return ibtn_left;
	}

	/**
	 * 可以是文字/图片资源
	 * 
	 * @param resid
	 */
	public ImageButton createRightImageButton(int drawable) {
		if (parentView instanceof Activity){
			ibtn_right = (ImageButton) ((Activity) parentView).findViewById(R.id.ibtn_top_right);
		}else{
			ibtn_right = (ImageButton) ((View) parentView).findViewById(R.id.ibtn_top_right);
		}
		if (drawable != 0) {
			ibtn_right.setVisibility(View.VISIBLE);
			ibtn_right.setImageResource(drawable);
		}
		return ibtn_right;
	}
	
	public void createNetworkView(){
		if (parentView instanceof Activity){
			v_network =((Activity) parentView).findViewById(R.id.rl_network);
		}else{
			v_network =((View) parentView).findViewById(R.id.rl_network);
		}
	}
	
	public void showNetworkView(Object alert){
		if (v_network==null) {
			createNetworkView();
		}
		if (alert!=null) {
			if (alert instanceof Integer) {
				if ((Integer) alert != 0) {
					((TextView) v_network.findViewById(R.id.tv_alertInfo)).setText((Integer) alert);
				}
			} else {
				((TextView) v_network.findViewById(R.id.tv_alertInfo)).setText((CharSequence) alert + "");
			}
		}
		v_network.setVisibility(View.VISIBLE);
	}
	
	public void hideNetworkView(){
		if (v_network!=null) {
			v_network.setVisibility(View.GONE);
		}
	}
	
	public TextView getTopTitle() {
		return tv_title;
	}

	public Button getLeftButton() {
		return btn_left;
	}

	public Button getRightButton() {
		return btn_right;
	}

	public ImageButton getLeftLocation() {
		return ibtn_left;
	}

	public ImageButton getRightLocation() {
		return ibtn_right;
	}
}